package com.chen.disruptor;

/**
 * @Author: chen
 * @Date: 2021/5/24 2:29 PM
 * @Version 1.0
 */

import com.chen.model.Order;
import com.chen.model.OrderEvent;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import lombok.extern.java.Log;

/**
 * 在boot里面使用它发送消息
 */
@Log
public class DisruptorTemplate {

    /**
     * 将Order对象 -》 注入到OrderEvent对象里面
     */
    private static final EventTranslatorOneArg<OrderEvent, Order> TRANSLATOR = new EventTranslatorOneArg<OrderEvent, Order>() {

        public void translateTo(OrderEvent event, long sequence, Order input) {
            event.setSource(input);
        }
    };
    private final RingBuffer<OrderEvent> ringBuffer;

    public DisruptorTemplate(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    /**
     * 发布事件 到ringBuffer 缓冲池
     * @param input
     */
    public void onData(Order input) {
        log.info("发布事件 到ringBuffer 缓冲池");
        ringBuffer.publishEvent(TRANSLATOR, input);
    }
}
